package LeetCode;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LC_539_MinimumTimeDifference {

    public static void main(String[] args) {

    }

    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            int result = Integer.MAX_VALUE;
            Set<String> set = new HashSet<>(timePoints);
            if (set.size() < timePoints.size())
                return 0;
            for (int i = 0; i < timePoints.size(); i++) {
                String[] firstTime = timePoints.get(i).split(":");
                int firstM = (Integer.valueOf(firstTime[0]) * 60 + Integer.valueOf(firstTime[1]));
                for (int j = i + 1; j < timePoints.size(); j++) {
                    String[] secondTime = timePoints.get(j).split(":");
                    int secondM = (Integer.valueOf(secondTime[0]) * 60 + Integer.valueOf(secondTime[1]));
                    int val = (secondM - firstM + 1440) % 1440;
                    int min = Math.min(val, 1440 - val);
                    result = Math.min(result, min);
                }
            }
            return result;
        }

    }

    public class Solution_1 {
        public int findMinDifference(List<String> timePoints) {
            boolean[] mark = new boolean[24 * 60];
            for (String time : timePoints) {
                String[] t = time.split(":");
                int h = Integer.parseInt(t[0]);
                int m = Integer.parseInt(t[1]);
                if (mark[h * 60 + m]) return 0;
                mark[h * 60 + m] = true;
            }

            int prev = 0, min = Integer.MAX_VALUE;
            int first = Integer.MAX_VALUE, last = Integer.MIN_VALUE;
            for (int i = 0; i < 24 * 60; i++) {
                if (mark[i]) {
                    if (first != Integer.MAX_VALUE) {
                        min = Math.min(min, i - prev);
                    }
                    first = Math.min(first, i);
                    last = Math.max(last, i);
                    prev = i;
                }
            }

            min = Math.min(min, (24 * 60 - last + first));

            return min;
        }
    }
}